﻿using System;
using System.Collections.Generic;
using Random = UnityEngine.Random;

namespace YummyGame.Framework
{
    public class SkillAction_random:SkillAction
    {
        private Expression threshold;
        private SkillAction success;
        private SkillAction failure;

        private SkillAction current;

        public override void Reactive()
        {
            success = null;
            failure = null;
            current = null;

            SkillActionPrototype_random prototype = Prototype as SkillActionPrototype_random;
            threshold = ParseRuntimeExpression(prototype.threshold);
            success = prototype.success?.ToRuntime(Executer);
            failure = prototype.failure?.ToRuntime(Executer);
        }

        public override ActionRunState Excute()
        {
            var value = threshold.Calculate();
            if (Random.Range(0, 1f) > value)
            {
                if (success == null) return ActionRunState.Success;
                current = success;
                return success.Excute();
            }
            else
            {
                if (failure == null) return ActionRunState.Success;
                current = failure;
                return failure.Excute();
            }
        }

        public override ActionRunState Run()
        {
            return current.Run();
        }

        public override void Dispose()
        {
            Pools.GetSkillRuntime<SkillAction_random>().Recover(this);
        }
    }
}
